package com.hk.Mgr.web.controller.order;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.order.TradeReq;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.*;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/trade")
public class TradeInfoCtrl extends BaseCtrl {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Resource
	private JdbcTemplate jdbcTemplate;

	/**
	 * 获得所有订单信息
	 *
	 * @return
	 * @author jeff
	 */
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public ResponseBase<List<Map<String, Object>>> listOrder(
			@RequestBody TradeReq req) {
		ResponseBase<List<Map<String, Object>>> resp = new ResponseBase<>();

		try {


			if (req == null || req.getActUserId() == null || req.getCurPage() == null || req.getPageSize() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				if (req.getActUserId() == null) {
					resp.setMsg("登录超时，请重新登！");
				} else {
					resp.setMsg("参数错误，请再次尝试！");
				}
				return resp;
			}

			Integer curPage = req.getCurPage();
			Integer pageSize = req.getPageSize();


			String ssn = req.getSsn();
			Integer type = req.getType();

			Integer payStatus = req.getPayStatus();
			String userName = req.getUserName();

			String beginDateStr = req.getBeginDate();
			String endDateStr = req.getEndDate();

			String sql = "select  t.id, t.login_id as phoneNum , t.userId, t.type, t.flowType, t.mchnt_txn_ssn, t.resp_Code, t.amt, t.orderId, t.tradeDesc, t.cts, t.channel, u.RealName  as userName   from t_trade_info t  left join SysUser u  on  t.userId = u.id ";

			String sqlFilter = " where 1=1";

			if (StringHelper.isNotEmpty(ssn)) {
				sqlFilter += "  and mchnt_txn_ssn = " + ssn;
			} else {
				if (StringHelper.isNotEmpty(userName)) {
					sqlFilter += " and (u.name like '%"
							+ userName
							+ "%\' "
							+ "or u.RealName like  '%"
							+ userName + "%\') ";
				}


				if (type != null) {
					sqlFilter += " and t.type = " + type;
				}

				if (payStatus != null) {
					if (payStatus == 1) {
						sqlFilter += " and t.resp_code = '0000'";
					} else if (payStatus == 0) {
						sqlFilter += " and t.resp_code != '0000'";
					}
				}

				// 增添日期筛选条件
				Date beginDate;
				Date endDate;
				SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
				HqlFilter hqlFilter = new HqlFilter();

				if (StringUtils.isNotEmpty(beginDateStr)) {
					beginDate = format.parse(beginDateStr);
					sqlFilter += " and cts > " + beginDate.getTime();
				}

				if (StringUtils.isNotEmpty(endDateStr)) {
					endDate = format.parse(endDateStr);

					Long endTimeLong = endDate.getTime() + 60 * 60 * 24 * 1000;

					sqlFilter += " and cts < " + endTimeLong;
				}


			}

			int firstResult = (req.getCurPage() - 1) * req.getPageSize();
			int maxSize = req.getPageSize();

			sqlFilter += " order by cts desc ";

			sqlFilter += " limit " + firstResult + ", " + maxSize;

			sql += sqlFilter;


			//logger.info("sql = {}", sql);
			List<Map<String, Object>> list = this.jdbcTemplate.queryForList(sql);

			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setSize(list.size());
			resp.setResult(list);

		} catch (Exception e) {
			logger.info("get tradeInfo exception e={}", e);
		}
		return resp;
	}
}

